<?
include "Devis.php";
include "Facture.php";
include "Line_devis.php";
include "Line_facture.php";
include "Paiement_client.php";
include "Stats.php";
class VentesController extends Controller {
    
	public $js = array("ventes",
		"jQuery/plugins/jqPlot/jquery.jqplot.min",
		"jQuery/plugins/jqPlot/jqplot.canvasAxisLabelRenderer.min",
	
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasTextRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisTickRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.categoryAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.barRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.highlighter.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.cursor.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min"
	);
	
	public $css = array("ventes","jQuery/jqPlot/jquery.jqplot.min");
	
	
	public function mainAction(){
		
		$connect = $this->connections;
		$devis = $connect->fetchAllModels("Devis",array("user_id"=>$_SESSION["user_id"]),array("date"=>"DESC"));
		$aContacts = $connect->fetchAllModels("Contacts",array("user_id"=>$_SESSION["user_id"]));
		$factures = $connect->fetchAllModels("Facture",array("user_id"=>$_SESSION["user_id"]),array("date"=>"DESC"));
		$typesReglements =  $connect->fetchAllModels("Type_reglement");
		$aTVA =  $connect->fetchAllModels("Tva");
		$planComptable =  $connect->fetchAllModels("Plan_comptable",array("code"=>array("LIKE"=>"7%")));
		$paiements =  $connect->fetchAllModels("Paiement_client",array(),array("paiement_client.date"=>"DESC"),"facture INNER JOIN paiement_client ON paiement_client._facture = facture._id");

		foreach($factures as $facture){
			$aDevisFactures[$facture->devis_id] = true;
		}
		foreach($paiements as $paiement){
			$aPaiementsClient[$paiement->_facture] = $factures[$paiement->_facture];
		}
		
		
		/**
		 * Statistiques
		 */
		// nombre de devis par mois
		$mysql = $connect->getConnection();
		$stats = new Stats($mysql);
		list($plot_nb_devis,$plot_nb_factures) = $stats->nb_devis();
		$ca_mensuel = $stats->ca_mensuel();
		
		
		$vars = array(
			"devis"=>$devis,
			"factures"=>$factures,
			"aContacts"=>$aContacts,
			"typesReglements"=>$typesReglements,
			"aTVA"=>$aTVA,
			"planComptable"=>$planComptable,
			"plot_nb_devis"=>$plot_nb_devis,
			"plot_nb_factures"=>$plot_nb_factures,
			"ca_mensuel"=>$ca_mensuel,
			"aDevisFactures"=>$aDevisFactures,
			"aPaiementsClient"=>$aPaiementsClient
		);
		return $this->render("ventes/main",$vars);
	}
	public function newDevisAction(){
		$connect = $this->connections;
		
		/** Si update 
		 * On supprime le devis pour le refaire*/
		if(isset($_POST["devis_id"])){
			$connect->remove("Devis",array("_id"=>$_POST["devis_id"]));
			$connect->remove("Line_devis",array("devis_id"=>$_POST["devis_id"]));
			$_POST["_id"] = $_POST["devis_id"];
		}
		/* Enregistrement du devis */
		$devis = new Devis();
		$_POST["date"] = date("Y-m-d",strtotime($_POST["date"])).date(" H:i:s");
		$_POST["echeance"] = date("Y-m-d H:i:s",strtotime($_POST["echeance"]));
		$devis->fromArray($_POST);
		$devis->user_id = $_SESSION["user_id"];
		// calcule du numéro du devis
		$_id = $connect->save($devis);
		if(!isset($_POST["_id"])){
			$devis->_id = $_id;
		}
		
		/* Enregistrement des lignes */
		foreach($_POST["lines"] as $line){
			if($line["qte"] != ""){
				$lineDevis = new Line_devis();
				$lineDevis->qte = $line["qte"];
				$lineDevis->description = $line["description"];
				$lineDevis->montant_ht = $line["montant_ht"];
				$lineDevis->classe = $line["plan"];
				$lineDevis->taux_tva = $line["taux_tva"];
				$lineDevis->devis_id = $devis->_id;
				$connect->save($lineDevis);
			}
		}
		return json_encode(array("response_code"=>"ok"));
	}
	public function updateEcheanceFactureAction(){
		
		$connect = $this->connections;
		$mysql = $connect->getConnection();
		
		$new_echeance = date("Y-m-d",strtotime($_POST["echeance"]));
		$facture_id = $_POST["facture_id"];
		
		$query = "UPDATE facture SET echeance = \"$new_echeance\" WHERE _id = \"$facture_id\";";
		$mysql->query($query);
		return json_encode(array("response_code"=>"ok"));
	
	}
	public function updateDateFactureAction(){
		
		$connect = $this->connections;
		$mysql = $connect->getConnection();
		
		$new_date = date("Y-m-d",strtotime($_POST["date"]));
		$facture_id = $_POST["facture_id"];
		
		$query = "UPDATE facture SET date = \"$new_date\" WHERE _id = \"$facture_id\";";
		$mysql->query($query);
		return json_encode(array("response_code"=>"ok"));
	
	}
	public function downloadDevisAction($devis_id){
		
		$connect = $this->connections;
		
		$pdf = new PdfFile();
		$devis = $connect->fetchModels("Devis",array("_id"=>$devis_id));
		$user = $connect->fetchModels("Users",array("_id"=>$_SESSION["user_id"]));
		$typeReglement = $connect->fetchModels("Type_reglement",array("_id"=>$devis->type_reglement));
		$prefs = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		$contact = $connect->fetchModels("Contacts",array("_id "=>$devis->contact_id));
		$lines = $connect->fetchAllModels("Line_devis",array("devis_id"=>$devis_id));
		
		$vars = array(
			"devis"=>$devis,
			"pdf"=>$pdf,
			"user"=>$user,
			"typeReglement"=>$typeReglement,
			"prefs"=>$prefs,
			"contact"=>$contact,
			"lines"=>$lines
		);
		return $this->render("downloads/devis",$vars);
	}
	public function downloadFactureAction($facture_id){
		
		$connect = $this->connections;
		
		$pdf = new PdfFile();
		$facture = $connect->fetchModels("Facture",array("_id"=>$facture_id));
		$user = $connect->fetchModels("Users",array("_id"=>$_SESSION["user_id"]));
		$typeReglement = $connect->fetchModels("Type_reglement",array("_id"=>$facture->type_reglement));
		$prefs = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		$contact = $connect->fetchModels("Contacts",array("_id "=>$facture->contact_id));
		$lines = $connect->fetchAllModels("Line_facture",array("facture_id"=>$facture_id));
		
		$vars = array(
			"facture"=>$facture,
			"pdf"=>$pdf,
			"user"=>$user,
			"typeReglement"=>$typeReglement,
			"prefs"=>$prefs,
			"contact"=>$contact,
			"lines"=>$lines
		);
		return $this->render("downloads/facture",$vars);
	}
	public function getDevisAction($devis_id){
		$connect = $this->connections;
		$devis = $connect->fetchModels("Devis",array("_id"=>$devis_id));
		$lines = $connect->fetchAllModels("Line_devis",array("devis_id"=>$devis_id));
		$devis->lines = $lines;		
		return json_encode($devis);
	}
	public function removeDevisAction($devis_id){
		$connect = $this->connections;
		$connect->remove("Devis",array("_id"=>$devis_id));
		$connect->remove("Line_devis",array("devis_id"=>$devis_id));
		return json_encode(array("response_code"=>"ok")); 
	}
	public function removeFactureAction($facture_id){
		$connect = $this->connections;
		
		$connect->remove("Facture",array("_id"=>$facture_id));
		$connect->remove("Line_facture",array("facture_id"=>$facture_id));
		
		return json_encode(array("response_code"=>"ok")); 
	}
	public function enregistrerPaiementAction($facture_id){
		$connect = $this->connections;
		
		$paiement_client = new Paiement_client();
		$paiement_client->_facture = $facture_id;
		$paiement_client->date = date("Y-m-d H:i:s");
		
		$connect->save($paiement_client);
		
		return json_encode(array("response_code"=>"ok")); 
		
	}
	public function removePaiementAction($facture_id){
		$connect = $this->connections;
		
		$connect->remove("Paiement_client",array("_facture"=>$facture_id));
		
		return json_encode(array("response_code"=>"ok")); 
	}
	public function facturerDevisAction($devis_id){
		$connect = $this->connections;
		$devis = $connect->fetchModels("Devis",array("_id"=>$devis_id));
		$lines = $connect->fetchAllModels("Line_devis",array("devis_id"=>$devis_id));
		
		$facture = new Facture();
		$facture->fromArray((array)$devis);
		$facture->devis_id = $devis_id;
		$facture->facture_id = $facture->getNextID($connect->getConnection());
		$facture_id = $connect->save($facture);
		
		foreach($lines as $line){
			$line_facture = new Line_facture();
			$line_facture->fromArray((array)$line);
			$line_facture->facture_id = $facture_id;
			$connect->save($line_facture); 
		}
		return json_encode(array("response_code"=>"ok")); 
	}
	public function downloadRelanceAction($devis_id){
		$connect = $this->connections;
		
		$pdf = new PdfFile();
		$devis = $connect->fetchModels("Devis",array("_id"=>$devis_id));
		$facture = $connect->fetchModels("Facture",array("devis_id"=>$devis_id));
		$user = $connect->fetchModels("Users",array("_id"=>$_SESSION["user_id"]));
		$typeReglement = $connect->fetchModels("Type_reglement",array("_id"=>$devis->type_reglement));
		$prefs = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		$contact = $connect->fetchModels("Contacts",array("_id "=>$devis->contact_id));
		$lines = $connect->fetchAllModels("Line_devis",array("devis_id"=>$devis_id));
		
		$vars = array(
			"devis"=>$devis,
			"facture"=>$facture,
			"pdf"=>$pdf,
			"user"=>$user,
			"typeReglement"=>$typeReglement,
			"prefs"=>$prefs,
			"contact"=>$contact,
			"lines"=>$lines
		);
		return $this->render("downloads/relance",$vars);
	}
	public function previewDevisAction($devis_id){
		$connect = $this->connections;
		
		$devis = $connect->fetchModels("Devis",array("_id"=>$devis_id));
		$lines = $connect->fetchAllModels("Line_devis",array("devis_id"=>$devis_id));
		$contact = $connect->fetchModels("Contacts",array("_id"=>$devis->contact_id));
		return $this->render("preview/devis", array("devis"=>$devis,"contact"=>$contact,"lines"=>$lines));
	}
	public function previewFactureAction($facture_id){
		$connect = $this->connections;
		
		$facture = $connect->fetchModels("Facture",array("_id"=>$facture_id));
		$lines = $connect->fetchAllModels("Line_facture",array("facture_id"=>$facture_id));
		$contact = $connect->fetchModels("Contacts",array("_id"=>$facture->contact_id));
		return $this->render("preview/facture", array("facture"=>$facture,"contact"=>$contact,"lines"=>$lines));
	}
}
?>